package com.org.utils.page;

/**
 * 简单的分页控件
 * 
 * @date 2012.1.5
 * @version beta 0.4
 * @CopyRight(c)2012 Intergrity Ltd.
 */
@SuppressWarnings({ "rawtypes" })
public class SimplePage implements IPage {
	public static final int DEF_COUNT = 20;
	protected int totalCount = 0;
	protected int pageSize = 20;
	protected int pageNo = 1;

	/**
	 * 无参的构造方法
	 */
	public SimplePage() {
	}

	/**
	 * 有参的构造方法
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param totalCount
	 */
	public SimplePage(int pageNo, int pageSize, int totalCount) {
		if (totalCount <= 0) {
			this.totalCount = 0;
		} else {
			this.totalCount = totalCount;
		}
		if (pageSize <= 0) {
			this.pageSize = DEF_COUNT;
		} else {
			this.pageSize = pageSize;
		}
		if (pageNo <= 0) {
			this.pageNo = 1;
		} else {
			this.pageNo = pageNo;
		}
		if (this.pageNo > getTotalPage()) {
			this.pageNo = getTotalPage();
		}
	}

	/**
	 * 调整分页参数，使合理化
	 * 
	 */
	public void adjustPage() {
		if (totalCount <= 0) {
			totalCount = 0;
		}
		if (pageSize <= 0) {
			pageSize = DEF_COUNT;
		}
		if (pageNo <= 0) {
			pageNo = 1;
		}
		if (pageNo > getTotalPage()) {
			pageNo = getTotalPage();
		}
	}

	public int getPageNo() {
		return pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public int getTotalPage() {
		int totalPage = totalCount / pageSize;
		if (totalCount % pageSize != 0) {
			totalPage++;
		}
		return totalPage;
	}

	public boolean isFirstPage() {
		return pageNo <= 1;
	}

	public boolean isLastPage() {
		return pageNo >= getTotalPage();
	}

	public int getNextPage() {
		if (isLastPage()) {
			return pageNo;
		} else {
			return pageNo + 1;
		}
	}

	public int getPrePage() {
		if (isFirstPage()) {
			return pageNo;
		} else {
			return pageNo - 1;
		}
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 如果大于总页数就等于总页数
	 * 
	 * @param pageNo
	 */
	public void setPageNo(int pageNo) {
		if (pageNo > getTotalPage()) {
			this.pageNo = getTotalPage();
		} else {

			this.pageNo = pageNo;
		}
	}
}
